﻿using System;
using System.Collections.Generic;
using BLToolkit.Data;
using BLToolkit.DataAccess;
using PMIS.BLL.System;

namespace PMIS.BLL
{
	/// <summary>
	/// 所有数据表操作的基类
	/// </summary>
	/// <typeparam name="T">实体类</typeparam>
	public class BL_Common<T> : BL_Base where T : iEntity
	{
		#region 公共业务逻辑(CRUD)
		/// <summary>
		/// 获取指定ID的记录信息
		/// </summary>
		/// <param name="ID">记录ID</param>
		/// <returns></returns>
		public virtual T GetEntity(string ID)
		{
			if (string.IsNullOrEmpty(ID))
				return default(T);

			T result = default(T);
			DbManager dm = null;
			try
			{
				//	创建数据库实例
				dm = BL_DatabaseConnection.CreateConnection();

				//	获取记录信息
				result = new SqlQuery<T>(dm).SelectByKey(ID);
			}
			catch (Exception ex)
			{
				BL_ExceptionOperation.WriteException(ex, dm);
				result = default(T);
			}
			finally
			{
				if (dm != null)
				{
					dm.Close();
					dm.Dispose();
					dm = null;
				}
			}

			return result;
		}
		/// <summary>
		/// 保存记录信息
		/// </summary>
		/// <param name="item">记录信息对象</param>
		/// <returns>是否操作成功</returns>
		public virtual bool Save(T item)
		{
			if (item == null)
				return false;

			bool result = false;
			DbManager dm = null;
			try
			{
				//	创建数据库实例
				dm = BL_DatabaseConnection.CreateConnection();

				SqlQuery<T> sq = new SqlQuery<T>(dm);
				if (string.IsNullOrEmpty(item.ID))
				{
					//	新增
					item.ID = Guid.NewGuid().ToString("D");
					result = sq.Insert(item) > 0;
				}
				else
				{
					//	修改
					result = sq.Update(item) > 0;
				}
			}
			catch (Exception ex)
			{
				BL_ExceptionOperation.WriteException(ex, dm);
				result = false;
			}
			finally
			{
				if (dm != null)
				{
					dm.Close();
					dm.Dispose();
					dm = null;
				}
			}

			return result;
		}
		/// <summary>
		/// 删除记录信息
		/// </summary>
		/// <param name="ID">记录ID</param>
		/// <returns>是否操作成功</returns>
		public virtual bool Delete(string ID)
		{
			if (string.IsNullOrEmpty(ID))
				return false;

			bool result = false;
			DbManager dm = null;
			try
			{
				//	创建数据库实例
				dm = BL_DatabaseConnection.CreateConnection();

				result = new SqlQuery<T>(dm).DeleteByKey(ID) > 0;
			}
			catch (Exception ex)
			{
				BL_ExceptionOperation.WriteException(ex, dm);
				result = false;
			}
			finally
			{
				if (dm != null)
				{
					dm.Close();
					dm.Dispose();
					dm = null;
				}
			}

			return result;
		}
		/// <summary>
		/// 获取所有记录信息
		/// </summary>
		/// <returns></returns>
		public virtual List<T> GetAll()
		{
			List<T> result = null;
			DbManager dm = null;
			try
			{
				//	创建数据库实例
				dm = BL_DatabaseConnection.CreateConnection();

				//	删除记录
				result = new SqlQuery<T>(dm).SelectAll();
			}
			catch (Exception ex)
			{
				BL_ExceptionOperation.WriteException(ex, dm);
				result = null;
			}
			finally
			{
				if (dm != null)
				{
					dm.Close();
					dm.Dispose();
					dm = null;
				}
			}

			return result;
		}
		#endregion
	}
}